// admin/printer-manage/printer-manage.js
// const db = wx.cloud.database(); // <-- ***删除：不再直接访问数据库***

Page({
    data: {
      printers: [],
      loading: true,
      showAddDialog: false,
      newPrinter: {
        name: '',
        location: '',
        status: '正常',
        remark: ''
      }
    },
  
    onShow: function () {
      this.getPrinters();
    },
  
    // --- *** 修改：使用云函数获取列表 *** ---
    getPrinters: function () {
      this.setData({ loading: true });
      wx.cloud.callFunction({
        name: 'manageprinters',
        data: {
          action: 'getAdminList'
        }
      }).then(res => {
        if (res.result.data) {
          this.setData({
            printers: res.result.data,
            loading: false
          });
        } else {
          wx.showToast({ title: res.result.message || '加载失败', icon: 'none' });
          this.setData({ loading: false });
        }
      }).catch(err => {
        this.setData({ loading: false });
        wx.showToast({ title: '加载失败', icon: 'none' });
        console.error(err);
      });
    },
  
    // --- *** 修改：使用云函数切换状态 *** ---
    onStatusChange: function (e) {
      const printerId = e.currentTarget.dataset.id;
      const currentStatus = e.currentTarget.dataset.status;
      const newStatus = currentStatus === '正常' ? '维护中' : '正常';
  
      wx.showModal({
        title: '确认',
        content: `确定要将状态更改为 "${newStatus}" 吗？`,
        success: res => {
          if (res.confirm) {
            wx.showLoading({ title: '更新中...' });
            wx.cloud.callFunction({
              name: 'manageprinters',
              data: {
                action: 'updateStatus',
                printerId: printerId,
                data: {
                  status: newStatus
                }
              }
            }).then(res => {
              wx.hideLoading();
              if (res.result.errMsg && res.result.errMsg.includes('ok')) {
                wx.showToast({ title: '更新成功' });
                this.getPrinters();
              } else {
                wx.showToast({ title: res.result.message || '更新失败', icon: 'none' });
              }
            }).catch(() => {
              wx.hideLoading();
              wx.showToast({ title: '更新失败', icon: 'none' });
            });
          }
        }
      });
    },
  
    // 显示添加打印机弹窗
    showAddDialog: function () {
      this.setData({ showAddDialog: true });
    },
  
    // 关闭弹窗
    closeDialog: function () {
      this.setData({ showAddDialog: false });
    },
  
    // 处理输入
    onInputDialog: function(e) {
      const { field } = e.currentTarget.dataset;
      this.setData({
          [`newPrinter.${field}`]: e.detail.value
      });
    },
  
    // --- *** 修改：使用云函数添加打印机 *** ---
    onAddPrinter: function () {
      const { name, location } = this.data.newPrinter;
      if (!name || !location) {
          wx.showToast({ title: '名称和位置不能为空', icon: 'none' });
          return;
      }
      
      wx.showLoading({ title: '添加中...' });
      wx.cloud.callFunction({
        name: 'manageprinters',
        data: {
          action: 'add',
          data: this.data.newPrinter
        }
      }).then(res => {
        wx.hideLoading();
        if (res.result.errMsg && res.result.errMsg.includes('ok')) {
          wx.showToast({ title: '添加成功' });
          this.closeDialog();
          this.getPrinters();
        } else {
          wx.showToast({ title: res.result.message || '添加失败', icon: 'none' });
        }
      }).catch(() => {
          wx.hideLoading();
          wx.showToast({ title: '添加失败', icon: 'none' });
      });
    }
  });